篇首语:本文由编程笔记#小编为大家整理,主要介绍了ListView和EditText使用解决方案相关的知识,希望对你有一定的参考价值。
ListView的复用对于EditText的坑有不少,比如焦点丢失、值乱窜、滚动问题。本文通过两种方案来解决:
该问题主要体现在于,点击EditText的时候键盘弹出,但是输入却没有任何反应,需要再点击一次才能输入数据。产生的原因在于弹出键盘的时候触发了ListView的刷新,导致本来获取了焦点的EditText又失去了焦点。这个坑我曾在4.4机器上踩平过,5.0之后焦点的获取机制不一样,在我的项目中后改为了方案二来实现,因此没有仔细研究。
//android 4.4 代码
private int touchPosition = -1
@Override
public View getView(final int position, View convertView, ViewGroup parent)
ViewHolder holder = ViewHolder.get(context, convertView, null, R.layout.layout_item, position);
final Bean bean = getItem(position);
((TextView) holder.getView(R.id.tv_id)).setText(bean.id + "");
EditText etName = holder.getView(R.id.et_name);
etName.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
touchPosition = position;
return false;
);
etName.setText(bean.name);
if (touchPosition == position)
etName.requestFocus();
etName.setSelection(etName.length());
else etName.clearFocus();
return holder.getRootView();
思路简单粗暴,也就是给编辑框增加一个onTouch监听,当ListView发生刷新的时候重新设置焦点,但这个方案在5.0机器及以上失效。
package com.wastrel.edittext;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* 创建一个viewHolder
*/
public class ViewHolder
public static final String TAG = "ViewHolder";
private SparseArray
private View rootView;
private int position;
private Object tag;
public void setTag(Object tag)
this.tag = tag;
public Object getTag()
return tag;
/**
* 生成一个adapter的ViewHolder
*
* @param context
* @param parent
* @param layoutId
* @param position
*/
private ViewHolder(Context context, ViewGroup parent, int layoutId, int position)
this.rootView = LayoutInflater.from(context).inflate(layoutId, parent, false);
this.childViews &#61; new SparseArray<>();
this.position &#61; position;
rootView.setTag(this);
/**
* 获取一个viewHolder
*
* &#64;param context
* &#64;param parent
* &#64;param layoutId
* &#64;param attachToRoot 是否添加到parent中
*/
public ViewHolder(Context context, ViewGroup parent, int layoutId, boolean attachToRoot)
this.rootView &#61; LayoutInflater.from(context).inflate(layoutId, parent, attachToRoot);
this.childViews &#61; new SparseArray<>();
rootView.setTag(this);
/**
* 获得一个viewHolder
*
* &#64;param context
* &#64;param convertView
* &#64;param parent
* &#64;param layoutId
* &#64;param position
* &#64;return
*/
public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position)
ViewHolder holder;
if (null &#61;&#61; convertView)
holder &#61; new ViewHolder(context, parent, layoutId, position);
else
holder &#61; (ViewHolder) convertView.getTag();
return holder;
/**
* 获取根容器
*
* &#64;return
*/
public View getRootView()
return this.rootView;
/**
* 获取容器中的某个控件
*
* &#64;param id
* &#64;param
* &#64;return
*/
&#64;SuppressWarnings("unchecked")
public
View view &#61;childViews.get(id);
if (view!&#61;null)
return (T)view;
view&#61;rootView.findViewById(id);
if (null &#61;&#61; view)
throw new IllegalArgumentException("没有找到id为" &#43; rootView.getContext().getResources().getResourceEntryName(id) &#43; "的控件");
else
childViews.put(id, view);
return (T) view;
解决这个问题很容易联想到使用TextWatcher。对每个EditText增加一个TextWatcher在用户输入的时候去更新数据源得值&#xff0c;下次刷新的时候在set回去即可。这里为了节省篇幅&#xff0c;仅贴出关键代码。
&#64;Override
public View getView(final int position, View convertView, ViewGroup parent)
ViewHolder holder &#61; ViewHolder.get(context, convertView, null, R.layout.layout_item, position);
final Bean bean &#61; getItem(position);
((TextView) holder.getView(R.id.tv_id)).setText(bean.id &#43; "");
EditText etName &#61; holder.getView(R.id.et_name);
//这段代码主要是确保EditText只持有一个TextWatcher&#xff0c;因为如果每次都使用add会导致EditText持有很多TextWatcher&#xff0c;一旦文字发生变化&#xff0c;将会触发其所有的TextWatcher&#xff0c;这样一来不但没有解决问题&#xff0c;反而使问题更加严重。
MyTextWatcher textWatcher &#61; (MyTextWatcher) etName.getTag();
if (textWatcher &#61;&#61; null)
textWatcher &#61; new MyTextWatcher();
etName.addTextChangedListener(textWatcher);
etName.setTag(textWatcher);
//修正当前EditText应该绑定的对象。
textWatcher.update(bean);
etName.setText(bean.name);
return holder.getRootView();
class MyTextWatcher implements TextWatcher
private Bean bean;
public void update(Bean bean)
this.bean &#61; bean;
&#64;Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
&#64;Override
public void onTextChanged(CharSequence s, int start, int before, int count)
&#64;Override
public void afterTextChanged(Editable s)
bean.name &#61; s.toString();
为什么要使用LinearLayout来替代ListVIew&#xff1f;
ListView与EditText组合会频繁的引起ListView刷新&#xff0c;在弹出键盘的过程中&#xff0c;ListView会刷新3-4次&#xff0c;当EditText编辑框变化时也会触发刷新&#xff0c;浪费性能。而且EditText的各种状态在刷新过程中会出现乱窜或丢失&#xff0c;颇为头疼的焦点问题。如果使用LinearLayout这些问题都将迎刃而解。
但是问题来了LinearLayout可没有ListView的Adapter好使啊&#xff01;下面我们可以给LinearLayout模拟一个Adapter出来&#xff0c;方便使用。见代码&#xff1a;
package com.wastrel.edittext;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import java.util.List;
import java.util.Stack;
/**
* 基础adapter
* 所有子类必须实现&#64;link #convert(ViewHolder, Object, int)
*/
public abstract class BaseLinearLayoutAdapter<T> extends android.widget.BaseAdapter
public List
public Context context;
private int layoutId;
Stack
public LinearLayout container;
public BaseLinearLayoutAdapter(Context context, List
this.context &#61; context;
this.data &#61; data;
this.container &#61; container;
container.removeAllViews();
this.layoutId &#61; layoutId;
&#64;Override
public int getCount()
return null &#61;&#61; data ? 0 : data.size();
&#64;Override
public T getItem(int position)
return null &#61;&#61; data ? null : data.get(position);
&#64;Override
public long getItemId(int position)
return 0;
public void setList(List
this.data &#61; data;
notifyDataSetChanged();
&#64;Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder &#61; ViewHolder.get(context, convertView, parent, layoutId, position);
T t &#61; getItem(position);
convert(holder, t, position);
return holder.getRootView();
/**
* 实现数据赋值
*
* &#64;param holder
* &#64;param item
* &#64;param position
*/
public abstract void convert(ViewHolder holder, T item, int position);
//重新notifyDataSetChanged()来满足LinearLayout。
&#64;Override
public void notifyDataSetChanged()
//获取当前容器里面还有多少个可用View
int viewCount &#61; container.getChildCount();
int size &#61; getCount();
for (int i &#61; 0; i
getView(i, container.getChildAt(i), container);
else
//当大于的时候先从缓存里面取&#xff0c;如果没有执行getView(i,null,container)去创建一个。
View v &#61; null;
if (detachViews.size() > 0)
v &#61; detachViews.get(0);
detachViews.remove(0);
v &#61; getView(i, v, container);
container.addView(v);
//把容器里没有用到的View取出来放到缓存中。
if (viewCount > size)
for (int i &#61; viewCount - 1; i >&#61; size; i--)
detachViews.add(container.getChildAt(i));
container.removeViewAt(i);
上面这个Adapter简单的重写了notifyDataSetChange()来模拟ListView刷新的过程。
布局应满足&#xff1a;如果使用ScrollView则LinearLayout的方向应该是纵向的&#xff0c;如果使用HorizontalScrollView则LinearLayout的方向应该是横向的。
<ScrollView
android:layout_width&#61;"match_parent"
android:layout_height&#61;"wrap_content">
<LinearLayout
android:layout_width&#61;"match_parent"
android:orientation&#61;"vertical"
android:layout_height&#61;"match_parent"/>
ScrollView>
public class LinearAdapter extends BaseLinearLayoutAdapter<Bean>
public LinearAdapter(Context context, List
super(context, data, container, layoutId);
&#64;Override
public void convert(ViewHolder holder, Bean bean, int position)
((TextView) holder.getView(R.id.tv_id)).setText(bean.id &#43; "");
EditText etName &#61; holder.getView(R.id.et_name);
MyTextWatcher textWatcher &#61; (MyTextWatcher) etName.getTag();
if (textWatcher &#61;&#61; null)
textWatcher &#61; new MyTextWatcher();
etName.addTextChangedListener(textWatcher);
etName.setTag(textWatcher);
textWatcher.update(bean);
etName.setText(bean.name);
class MyTextWatcher implements TextWatcher
private Bean bean;
public void update(Bean bean)
this.bean &#61; bean;
&#64;Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
&#64;Override
public void onTextChanged(CharSequence s, int start, int before, int count)
&#64;Override
public void afterTextChanged(Editable s)
bean.name &#61; s.toString();
LinearAdapter adapter &#61; new LinearAdapter(this, beans, listView, R.layout.layout_item);
adapter.notifyDataSetChanged();
数据集变化的时候调用Adapter的notifyDataSetChanged()就好了。
上述方案适用于Item条数不多&#xff0c;并且用户可动态添加和删除条目的情况。动态删减的过程中任然避免不了通过TextWatcher来快速保存数据。但是此方案不会存在焦点问题&#xff0c;列表也不会反复刷新。如果条目固定的输入直接用for循环就好了。然后把ViewHolder缓存起来&#xff0c;就可以解决大部分问题。